package service

import (
	"chat/log"
	"chat/models"
	"chat/utils"

	"github.com/gin-gonic/gin"
	"strconv"
)

func AddNotice(c *gin.Context) {
	var notice models.Notice
	if err := c.ShouldBindJSON(&notice); err != nil {
		log.Error("参数错误")
		utils.RespFail(c.Writer, "参数错误")
		return
	}
	if err := models.CreateNotice(&notice); err != nil {
		log.Error("创建公告失败")
		utils.RespFail(c.Writer, "创建公告失败")
		return
	}
	utils.RespOk(c.Writer, "", "创建公告成功")
}

func GetNotices(c *gin.Context) {
	id := c.Query("id")
	data, err := models.GetNoticeList(id)
	if err != nil {
		utils.RespFail(c.Writer, "发生未知错误")
		return
	}
	utils.RespOkList(c.Writer, data, len(data))
}

func DelNotice(c *gin.Context) {
	id := c.Query("id")
	n, _ := strconv.Atoi(id)
	if err := models.DeleteNotice(uint(n)); err != nil {
		log.Error("删除公告失败")
		utils.RespFail(c.Writer, "删除公告失败，操作数据库失败")
		return
	}
	utils.RespOk(c.Writer, "", "删除成功")
}
